<?php

namespace App\Http\Controllers;

use App\RunGameLogs;
use App\RunGamePrize;
use App\RunGameRank;
use App\RunGameReward;
use App\RunGameSeason;
use App\User;
use DB;

/**
 * Class RunGameController
 * @package App\Http\Controllers
 */
class RunGameController extends Controller
{
    /**
     * @var
     */
    private $user_id;

    /**
     * constructor.
     */
    public function __construct()
    {
        $this->middleware('checkuuid')->only('startGame', 'endGame', 'acceptPrice');
        $this->user_id = User::CheckUserUuid(request('user_id'));
    }


    /**
     * 配置参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function config()
    {
        $config = DB::table('run_game_config')
            ->select('integral_pre', 'rule_content', 'home_title','home_pole', 'home_role_1', 'home_role_2', 'home_role_3', 'home_bg', 'home_start', 'home_price', 'home_price1', 'home_price2', 'home_price3', 'gaming_box', 'gaming_trap', 'gaming_bg', 'gaming_dashed', 'gaming_true', 'gaming_true_after', 'gaming_false', 'gaming_false_after', 'rank_bg_all', 'rank_bg_self', 'answer_box', 'answer_back', 'answer_true', 'answer_success', 'rule_bg', 'close', 'game_times')
            ->find(1);
        $roles = DB::table('run_game_roles')->pluck('path')->toArray();
        $coins = User::where('c_id', $this->user_id)->value('coin_number');
        //今天是否还能玩
        if (RunGameLogs::whereBetween('start_at', RunGameLogs::Today())->where('times', '<=', 0)->where('user_id', $this->user_id)->exists()) {
            $times = 0;
        } else {
            $times = 1;
        }
        //分享添加游戏次数
        if (request()->has('shareId') && request('shareId') !== request('user_id')) {
            $share_id = User::CheckUserUuid(request('shareId'));
            $log = RunGameLogs::select('id', 'has_share')->where('user_id', $share_id)->whereBetween('start_at', RunGameLogs::Today())->first();
            $add = success();
            if (!$log) {
                $add = RunGameLogs::NewAddTimes($share_id);
            } else if ($log->has_share === RunGameLogs::SHARE_NO) {
                $add = RunGameLogs::AddTimes($log->id);
            }
            if ($add['code'] !== 200) {
                return response()->json(error('游戏出错'));
            }
        }

        return response()->json(success(compact('config', 'roles', 'coins','times')));
    }


    /**
     * 开始游戏&&出题
     * @return \Illuminate\Http\JsonResponse
     */
    public function startGame()
    {
        //今天是否还能玩
        if (RunGameLogs::whereBetween('start_at', RunGameLogs::Today())->where('times', '<=', 0)->where('user_id', $this->user_id)->exists()) {
            return response()->json(error('您今天已经没有游戏次数了'));
        }
        //出题
        return response()->json(RunGameLogs::SendQuestion($this->user_id));
    }

    /**
     * 结束游戏
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function endGame()
    {
        //校验游戏数据
        if (!RunGameLogs::where('id', request('log_id'))->where('user_id', $this->user_id)->exists()) {
            return response()->json(error('数据有误'));
        }
        //校验题目ID
        $items = explode(',', request('item_ids'));
        if (array_diff(array_unique($items), $items)) {
            return response()->json(error('数据有误'));
        }

        if (intval(request('victory')) === 1) {
            $integral = count($items);
        } else {
            $integral = count($items) - 1;
        }
        //结束
        return response()->json(RunGameLogs::EndGame($this->user_id, $integral));
    }

    /**
     * 排行榜
     * @return array
     */
    public function rank()
    {
        $thisSeason = RunGameSeason::ThisSeason();
        if (!$thisSeason) {
            $thisSeason = RunGameSeason::orderByDesc('end_at')->value('id');
        }
        return response()->json(RunGameRank::Rank($this->user_id, $thisSeason));
    }

    /**
     * 奖品列表
     * @return array
     */
    public function rewards()
    {
        return response()->json(RunGameReward::Rewards($this->user_id));
    }

    /**
     * 奖品详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function rewardInfo()
    {
        return response()->json(RunGameReward::RewardInfo($this->user_id));
    }

    /**
     * 领取奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function acceptPrice()
    {
        //检验参数
        if (!request()->has('name') ||
            !request()->has('mobile') ||
            !request()->has('province') ||
            !request()->has('city') ||
            !request()->has('county') ||
            !request()->has('address')) {
            return response()->json(error('缺少参数'));
        }
        //校验手机号
        if (!mobile(request('mobile'))) {
            return response()->json(error('手机号不正确'));
        }
        $user_id = $this->user_id;
        //判断是否有该订单
        $price_limit = DB::table('run_game_config')->value('price_limit') * 24 * 3600;
        if (!RunGamePrize::where('id', request('price_id'))->where('user_id', $user_id)->where('status', RunGamePrize::STATUS_WAITTING)->where('created_at','>=', time() - $price_limit)->exists()) {
            return response()->json(error('订单出错'));
        }

        //修改订单
        return response()->json(RunGamePrize::AcceptPrice(request('price_id')));
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserId()
    {
        if (!request()->has('code')) {
            return response()->json(error('没有code'));
        }
        return response()->json(User::GetUserId(request('code')));
    }

}
